package com.huangliusong.desigin.pattern.creational.singleton;

import java.io.Serializable;

/**
 * 饿汉式单利模式
 * @author huangliusong
 * @since 2018/10/10
 * {@link static}
 */
public class HungrySingleton implements Serializable,Cloneable {
    private final  static HungrySingleton hungrySingleton;

    //静态代码块
   static {
       hungrySingleton=new HungrySingleton();
   }
    //static
    public static HungrySingleton getInstance(){
        return hungrySingleton;
    }
    private HungrySingleton() {
        if (hungrySingleton!=null){
            throw new RuntimeException("单li构造器禁止进行反射");
        }
    }

    private Object readResolve(){
       return hungrySingleton;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return getInstance();
    }
}
